package com.vincent.rocketmqdemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.aliyun.openservices.ons.api.exception.ONSClientException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@RequestMapping("/test")
 public class TestController {

    private static final String topic = "INSNAIL_MESSAGE_TOPIC_DEV";
    private static final String tag = "NOTICE_MESSAGE";
    private static final String emailTag = "EMAIL_MESSAGE";

     @Resource(name = "demoProducer")
     private ProducerBean producer;

     @RequestMapping("/send")
     public void sendMassage(@RequestBody JSONObject json) {
//         Message msg = getMessage(json, "T-SNAIL-LIFE-ORDER-CREATE", "*");
//         Message msg = getMessage(json, "cms_acticle_dev", "Article");
//         Message msg = getMessage(json, "chat_event_dev", "switchUser");
         Message msg = getMessage(json, "T-SNAIL-LIFE-NUCLEAR-INSURANCE", "*");
         try {
             SendResult sendResult = producer.send(msg);
             assert sendResult != null;
             System.out.println("send success: " + sendResult.getMessageId());
         }catch (ONSClientException e) {
             e.printStackTrace();
             System.out.println("发送失败");
         }

     }


    @RequestMapping("/batchSendTest")
    public void batchSendTest() {
         JSONObject json = new JSONObject();

         json.put("msgJson", new JSONObject());
         json.put("phone", "13667737445");
         for (int i = 1; i <= 75; i++) {
             json.put("templateCode", i);
             Message msg = getMessage(json, topic, tag);
             msg.setKey("ORDERID_100");
             try {
                 SendResult sendResult = producer.send(msg);
                 assert sendResult != null;
                 System.out.println("send success: " + sendResult.getMessageId());
             }catch (ONSClientException e) {
                 System.out.println(i+"发送失败");
             }
         }
     }

    private Message getMessage(JSONObject json, String topic, String tag) {
        return new Message(topic, tag, json.toJSONString().getBytes());
    }

 }